.empty()Returns: jQuery
Description: Remove all child nodes of the set of matched elements from the DOM.
This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child node of that element. Consider the following HTML:
1
2
3
4
|
|
We can target any element for removal:
1
|
|
This will result in a DOM structure with the Hello
text deleted:
1
2
3
4
|
|
If we had any number of nested elements inside <div class="hello">
, they would be removed, too.
To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.
If you want to remove elements without destroying their data or event handlers (so they can be re-added later), use .detach()
instead.
Example:
Removes all child nodes (including text nodes) from all paragraphs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
|